package zj.it.bhne.gridengineeringsurvey.myutils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import zj.it.bhne.gridengineeringsurvey.R;

/**
 * 项目中常用到的工具类
 */
public class MyUtils {

    private static Toast toast;

    /**
     * 显示Toast
     */
    @SuppressLint("ShowToast")
    public static void getToast(Context context, String content) {
        if (toast == null)
            toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
         else
            toast.setText(content);

        toast.show();
    }

    /**
     * 壓縮Bitmap
     */
    public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
        //获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        //计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        //取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        //得到新的图片
        return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
    }


    /**
     * 比较两个日期的大小，日期格式为yyyy-MM-dd
     *
     * @param str1 the first date
     * @param str2 the second date
     * @return true <br/>false
     */
    public static boolean isDateOneBigger(String str1, String str2) {
        boolean isBigger = false;
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt1 = null;
        Date dt2 = null;
        try {
            dt1 = sdf.parse(str1);
            dt2 = sdf.parse(str2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        assert dt2 != null;
        if (dt1.getTime() > dt2.getTime()) {
            isBigger = true;
        } else if (dt1.getTime() < dt2.getTime()) {
            isBigger = false;
        }
        return isBigger;
    }


    public static void getProgressBar(Context context){

    }

    public static final String DATABASE_FILENAME = "bhnesd.db";                // 这个是DB文件名字
    public static final String PACKAGE_NAME = "zj.it.bhne.gridengineeringsurvey";    // 这个是自己项目包路径
    public static final String DATABASE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/"
            + PACKAGE_NAME;    // 获取存储位置地址
    //点ID
    public static long bid;
    //项目ID
    public static long zid;
    static String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
    /**
     * 查询外面导入的数据库中的信息
     */
    @Nullable
    public static SQLiteDatabase openDatabase(Context context) {
        try {
//			String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
            File dir = new File(DATABASE_PATH);
            if (!dir.exists()) {
                dir.mkdir();//新建文件
            }
            if (!(new File(databaseFilename)).exists()) {
                InputStream is = context.getResources().openRawResource(
                        R.raw.bhnes);
                FileOutputStream fos = new FileOutputStream(databaseFilename);
                byte[] buffer = new byte[8192];
                int count;
                while ((count = is.read(buffer)) > 0) {
                    fos.write(buffer, 0, count);
                }

                fos.close();
                is.close();
            }
            Log.e("path",databaseFilename);
            return SQLiteDatabase.openOrCreateDatabase(databaseFilename,
                    null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取两经纬度点之间的直线距离
     */
    public static Double Distance(double lat1, double lng1, double lat2, double lng2) {

        Double R = 6370996.81;  //地球的半径
        //获取两点间x,y轴之间的距离
        Double x = (lng2 - lng1) * Math.PI * R * Math.cos(((lat1 + lat2) / 2) * Math.PI / 180) / 180;
        Double y = (lat2 - lat1) * Math.PI * R / 180;
        //得到两点之间的直线距离

        return Math.hypot(x, y);
    }

}
